package tree.tree2.symmetry.BinarySearchTree;

import tree.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName GetMinimumDifference
 * @Description TODO
 * @Author lenovo
 * @Date 2023-05-08 9:36
 * @Version 1.0
 * @Comment Magic. Do not touch.
 * If this comment is removed. the program will blow up
 */
public class GetMinimumDifference {
    /**
     * 530. 二叉搜索树的最小绝对差
     * 给你一个二叉搜索树的根节点 root ，返回 树中任意两不同节点值之间的最小差值 。
     * <p>
     * 差值是一个正数，其数值等于两值之差的绝对值。
     */

    List<Integer> result = new ArrayList<>();

    public int getMinimumDifference(TreeNode root) {
        inOrder(root);

        Integer minVal = Integer.MAX_VALUE;
        for (int i = 1; i < result.size(); i++) {
            minVal = Math.min(result.get(i) - result.get(i - 1), minVal);
        }
        return minVal;
    }

    /**
     *     2
     *  1    3
     * @param root
     */
    private void inOrder(TreeNode root) {
        if (root == null) return;

        inOrder(root.left);

        result.add(root.val);

        inOrder(root.right);
    }

    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(2);
        treeNode.left = new TreeNode(1);
        treeNode.right = new TreeNode(4);

        GetMinimumDifference getMinimumDifference = new GetMinimumDifference();
        System.out.println(getMinimumDifference.getMinimumDifference(treeNode));
    }
}